# coding: utf-8
import login
import menu


def append_shopping_list(dict_shopping_list, goods_info):
    """

    :param dict_shopping_list: 传入字典类型的购物列表
    :param goods_info: 用户购买的信息 [物品名称,数量]
    :return: None
    """

    # 首先检查用户购买的商品是否属于本次重复购买
    goods_name = goods_info[0]
    goods_num = int(goods_info[1])
    if dict_shopping_list.get(goods_name):
        # 已经购买过, 只需增加数量即可
        dict_shopping_list[goods_name] += goods_num
    else:
        # 字典中没有找到,表示本次购物没有买过相同的商品
        dict_shopping_list[goods_name] = goods_num

    # 将更新过的购物车列表返回
    return dict_shopping_list


shopping_list = {}

customer = login.login()

while True:
    print("买买买".center(22, '-'))
    user_input = menu.list_first_menu()
    if user_input == 'Q':
        exit("Bye")
    elif user_input == 'C':
        history_shopping_list = customer.check_shopping_history()
        print('\t\t', "购物时间", '\t\t', '商品名称', '\t\t', '单次购买数量')
        for i in history_shopping_list:
            print(i[0], '\t', i[1], '\t', i[2])
    elif user_input == 'T':
        money = input("输入充值金额: ").strip()
        if money.isdigit():
            new_money = customer.recharge(money)
            print("最新余额: ", new_money)
        else:
            print("输入异常")
    elif user_input == 'L':
        for i in shopping_list:
            print(i, shopping_list[i])
    elif user_input.isdigit():
        user_input = menu.list_second_menu(user_input)
        if user_input == 'Q':
            exit("Bye")
        elif user_input == 'C':
            history_shopping_list = customer.check_shopping_history()
            print('\t\t', "购物时间", '\t\t', '商品名称', '\t\t', '单次购买数量')
            for i in history_shopping_list:
                print(i[0], '\t', i[1], '\t', i[2])
        elif user_input == 'T':
            money = input("输入充值金额: ").strip()
            if money.isdigit():
                new_money = customer.recharge(money)
                print("最新余额: ", new_money)
            else:
                print("输入异常")
        elif user_input == 'B':
            continue
        elif user_input == 'L':
            for i in shopping_list:
                print(i, shopping_list[i])
        elif type(user_input) == list:
            print("更新用户购物记录")
            # 更新用户购物车
            customer.update_shop_list([user_input[0], user_input[1], user_input[3]])
            print("扣款")
            # 扣款
            if customer.update_balance(user_input[2], user_input[3]):
                shopping_list = append_shopping_list(shopping_list, [user_input[1], user_input[3]])
                print("购买成功! 可以继续败家")
            else:
                print("哎呦,钱不够了!按[T]可以充值哦!瞬间到账!")
                continue
        else:
            continue
    else:
        print("操作字符不合法")
        continue
